//
//  HomeTableView.m
//  GameBaby
//
//  Created by keyZhang on 13-12-19.
//  Copyright (c) 2013年 keyZhang. All rights reserved.
//


#import "HomeTableView.h"
#import "DataService.h"
#import "HomeTableViewCell.h"
#import "GameCenterViewController.h"
#import "WebShowViewController.h"

@interface HomeTableView ()
{
    NSMutableArray *hImgNameArray;
    NSMutableArray *hURLArray;
}

@end

@implementation HomeTableView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    self.rowHeight = 80;
    [self loadData];
    self.isMore = NO;
//    [self initHeaderView];

}

- (void)loadData
{
    __weak HomeTableView *this = self;
    [DataService requestWithURL:@"http://m.a.ewoka.com/init/index/"
                         params:nil
                     httpMethod:@"GET"
                    finishBlock:^(id result) {
                        [this loadDataFinished:result];
                    }];
}

- (void)loadDataFinished:(NSDictionary *)result
{
    NSArray *array = [result objectForKey:@"alideshow"];
    hImgNameArray = [NSMutableArray array];
    hURLArray = [NSMutableArray array];
    for (NSDictionary *dic in array) {
        NSString *imgUrl = [dic objectForKey:@"litpic"];
        NSString *urlLink = [dic objectForKey:@"arcurl"];
        [hImgNameArray addObject:imgUrl];
        [hURLArray addObject:urlLink];
    }
    [self initHeaderView];

}


- (void)initHeaderView
{
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 150)];
    _scrollView.contentSize = CGSizeMake(320 * 5, 150);
    _scrollView.bounces = YES;
    _scrollView.pagingEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.delegate = self;
    self.tableHeaderView = _scrollView;
    
    for (int i = 0; i < 5; i ++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(320 * i, 0, 320, 150)];
        imageView.tag = 50+i;
        UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        imageView.userInteractionEnabled = YES;
        [imageView addGestureRecognizer:tapRecognizer];
        [imageView setImageWithURL:[NSURL URLWithString:hImgNameArray[i]]];
        [_scrollView addSubview:imageView];
    }
    
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 133, 320, 20)];
    pageControl.enabled = NO;
    pageControl.tag = 100;
    pageControl.numberOfPages = 5;
    [self addSubview:pageControl];
    
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
}

- (void)tapAction:(UITapGestureRecognizer *)tap
{
    WebShowViewController *wbVC = [[WebShowViewController alloc] init];
    wbVC.linkURL = hURLArray[tap.view.tag - 50];
    wbVC.contentTitle = @"精品推荐";
    
    [self.viewController.navigationController pushViewController:wbVC animated:YES];
}


static int count = 0;
- (void)timerAction:(NSTimer *)timer
{
    UIPageControl *pageControl = (UIPageControl *)[self viewWithTag:100];
    count = pageControl.currentPage;
    pageControl.currentPage++;
    count ++;
    if (count == 5) {
        pageControl.currentPage = 0;
        [(UIScrollView *)self.tableHeaderView setContentOffset:CGPointMake(0, 0) animated:NO];
    }else
    {
        [(UIScrollView *)self.tableHeaderView setContentOffset:CGPointMake(320 * count , 0) animated:YES];
    }
    
}

#pragma mark - UIScrollViewDelegate
- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    UIPageControl *pageControl = (UIPageControl *)[self viewWithTag:100];
    pageControl.currentPage = scrollView.contentOffset.x/320;

    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
}

- (void) scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    [self.timer invalidate];
}



#pragma mark -UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"homeCell";
    HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"HomeTableViewCell" owner:self options:nil] lastObject];
    }
    
    cell.model = self.dataList[indexPath.row];
    
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    GameCenterViewController *gameCenterVC = [[GameCenterViewController alloc] init];
    gameCenterVC.gameXqUrl = [(HomeCellModel *)self.dataList[indexPath.row] url];
    [self.viewController.navigationController pushViewController:gameCenterVC animated:YES];
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
